#!/bin/ash -efu
#
# mki-pack-squash
#
# This file is part of mkimage
# Copyright (C) 2007-2009  Alexey Gladkov <gladkov.alexey@gmail.com>
#
# This file is covered by the GNU General Public License,
# which should be included with mkimage as the file COPYING.
#

. "${0%/*}"/mki-sh-functions

verbose "has started executing."

nproc="${PACK_SQUASHFS_PROCESSORS:-}"
nproc="$(opt_check_number 'PACK_SQUASHFS_PROCESSORS' "$nproc")"
pkgs="${PACKAGES_REQUIRED_PACK_SQUASHFS:-}"
opts="${PACK_SQUASHFS_OPTS:-}"
mksquashfs_prog="${MKSQUASHFS:?MKSQUASHFS must be specified}"

outname="${MKI_OUTNAME:-outname}"
outname="${outname##*/}"

imgsubdir="${MKI_IMAGESUBDIR:-}"
imgsubdir="${imgsubdir#/}"

exclude=
for d in ${MKI_EXCLUDE:-}; do
	exclude="$exclude ${d#/}"
done

[ -d "$chroot" ] ||
	fatal "$dir: not look like a work directory of hasher."

mki-install $pkgs ||
	fatal "failed to install packages: $pkgs."

HSH_PROC=1 run_chrooted "$chroot" <<EOF
#!/bin/sh -efu

imgdir=/.image
[ -z "$imgsubdir" ] || [ ! -d "/.image/$imgsubdir" ] ||
	imgdir="/.image/$imgsubdir"

[ ! -e '/.our/$outname' ] ||
	rm -f -- '/.our/$outname'

$mksquashfs_prog "\$imgdir/" '/.our/$outname' $opts -no-recovery \\
	${nproc:+-processors $nproc} -e .in .host .fakedata $exclude
EOF

mki-copy-our2out
